package cn.exrick.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.exrick.mapper.TbItemSkuGoodCatAttrMapper;
import cn.exrick.pojo.TbItemSkuGoodCat;
import cn.exrick.pojo.TbItemSkuGoodCatAttr;
import cn.exrick.service.ItemSkuGoodCatAttrService;
import cn.exrick.service.ItemSkuGoodCatService;

@Service
public class ItemSkuGoodCatAttrServiceImpl extends BaseServiceImpl<TbItemSkuGoodCatAttr, TbItemSkuGoodCatAttrMapper>
		implements ItemSkuGoodCatAttrService {
	private final static Logger log = LoggerFactory.getLogger(ItemSkuGoodCatAttrServiceImpl.class);

	@Override
	public Logger getLogger() {
		return log;
	}

	@Autowired
	TbItemSkuGoodCatAttrMapper tbItemSkuGoodCatAttrMapper;

	@Autowired
	ItemSkuGoodCatService itemSkuGoodCatService;

	@Override
	public TbItemSkuGoodCatAttrMapper getMapper() {
		return tbItemSkuGoodCatAttrMapper;
	}

	public TbItemSkuGoodCatAttr findByItemId(long itemId) {
		return findByUniqueForOne("item_id", String.valueOf(itemId));
	}

	@Override
	public List<TbItemSkuGoodCatAttr> findByCatId(long catId) {
		return findByUnique("item_sku_good_cat_id", String.valueOf(catId));
	}

	@Override
	public List<TbItemSkuGoodCat> findOneTreeByAttrId(long attrId) {
		List<TbItemSkuGoodCat> result = new ArrayList<>();
		loop(attrId, result);
		return result;
	}

	private void loop(long attrId, List<TbItemSkuGoodCat> result) {
		TbItemSkuGoodCatAttr itemSkuGoodCatAttr = findById(attrId);
		if (itemSkuGoodCatAttr == null) {
			return;
		}
		TbItemSkuGoodCat itemSkuGoodCat = itemSkuGoodCatService.findById(itemSkuGoodCatAttr.getItemSkuGoodCatId());

		if (itemSkuGoodCat != null) {
			itemSkuGoodCat.setAttr(itemSkuGoodCatAttr);
		}
		result.add(itemSkuGoodCat);
		if (itemSkuGoodCat.getFatherItemSkuGoodCatId() != null) {
			loop(itemSkuGoodCat.getFatherItemSkuGoodCatId(), result);
		}
	}

}
